package ink.lovejinhu.leetcode;

import java.util.Stack;

/**
 * @Author: jin
 * @Date: 2021/9/2 17:51
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Solution_0844 {
    /**
     * 字符串里含回格
     * stringbuffer中的deleteCharAt方法
     *
     * @param s
     * @param t
     * @return
     */
    public boolean backspaceCompare(String s, String t) {
        String lastString = this.getLastString(s);
        String lastString1 = this.getLastString(t);
        return lastString1.equals(lastString);

    }

    //0145467291
    public String getLastString(String s) {
        Stack<Character> characters = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i))) {
                characters.push(s.charAt(i));
            } else {
                if (!characters.isEmpty()) {
                    characters.pop();
                }
            }
        }
        String res = "";
        while (!characters.isEmpty()) {
            res = characters.pop() + res;
        }
        return res;
    }
}
